<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<header th:replace="inc/header"></header>

<body th:with="menu='task'">
<nav th:replace="inc/nav"></nav>
<style>
	.ruleTextList{
		margin-right:100px;
		float:right;
		display:inline-block;
		width:200px;
	}
	
</style>
<div class="container-fluid">
  <div class="row">
    <div th:replace="inc/sidebar"></div>
    <div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
      <h1 class="page-header">
        	定时任务
        <div class="pull-right">
          <a class="btn btn-primary btn-sm" id="taskAdd">新增</a>
        </div>
      </h1>
      <table class="table table-striped taskTable">
        <thead>
        <tr>
          <th>任务名称</th>
          <th>任务类型</th>
          <th>任务规则</th>
          <th>创建时间</th>
          <!-- <th>最后同步时间</th> -->
          <th>运行状态</th>
          <th>操作</th>
        </tr>
        </thead>
        <tbody>

        </tbody>
      </table>
    </div>
  </div>
</div>

<div class="modal fade" id="taskSave" tabindex="-1" role="dialog" data-backdrop="static">
  <div class="modal-dialog modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
          aria-hidden="true">&times;</span></button>
        <h4 class="modal-title" id="taskmodalTitle">添加定时任务</h4>
      </div>
      <div class="modal-body" style="border-bottom:1px solid #e5e5e5;">
        <form class="form-horizontal" id="taskformSave">
          <input type="hidden" name="jsbh" id="jsbh" th:attr="value=${jsbh}">
          <input type="hidden" name="id" id="id">
          <div class="form-group">
            <label for="name" class="control-label col-md-3">任务名称</label>
            <div class="col-md-7">
              <input type="text" name="name" id="taskname" class="form-control" required>
            </div>
          </div>
          <div class="form-group">
            <label for="name" class="control-label col-md-3">执行规则</label>
            <div class="col-md-7">
              <input type="text" name="cronExpress" id="taskrule" class="form-control" required>
            </div>
          </div>
          
          <div class="form-group" style="margin-left: 124px;">
            <!-- <label for="name" class="control-label col-md-3">定时点名</label> -->
            <div class="col-md-3">
              <span style="">定时点名</span>
              <input type="radio" name="orderName" value="rollcall" id="rollcallRadio" checked>
            </div>
            <div class="col-md-3">
            	<span style="">定时值班</span>
              <input type="radio" name="orderName" value="shift" id="shiftRadio">
            </div>
          </div>
          <div class="form-group">

          </div>
        </form>
      </div>
      <div style="padding-left: 30px;">
      	<h4>任务规则举例说明</h4>
      	<p>每分钟执行一次<span class="ruleTextList">*/1 * * * *</span></p>
      	<p>每个小时执行一次<span class="ruleTextList">1 */1 * * *</span> </p>
      	<p>每天23:59执行一次<span class="ruleTextList">59 23 * * *</span> </p>
      	<p>每天凌晨10分执行一次 <span class="ruleTextList">10 0 * * *</span></p>
      	<p>每月最后一天的23:59执行<span class="ruleTextList">29 23 L * *</span> </p>
      	<p>每天的9点到晚6点，每个小时执行一次 <span class="ruleTextList">1 9-18/1 * * *</span></p>
      	<p>每天的早晨9点和晚上6点各执行一次 <span class="ruleTextList">1 9,18 * * *</span></p>
      	<p>每天的早晨9点到晚上6点以后，每半个小时执行一次 <span class="ruleTextList">*/30 9-18 * * *</span></p>
      	<p>每天的晚上10点到0点，每5分钟执行一次 <span class="ruleTextList">*/5 22-0 * * *</span></p>
      	<p>每天的10点、12点、14点、16点、18点、20点、22点和0点各执行一次 <span class="ruleTextList">1 10,12,14,16,18,20,22,0 * * *</span></p>
      	
      	
      </div>
<!--       <div style="padding-left: 30px;">
      	<h3>任务规则举例说明</h3>
      	<p>每分钟执行一次</p>
      	<p>* * * * * </p>
      	<p>*/1 * * * *</p>
      	<br/>
      	<p>每个小时执行一次 </p>
      	<p>1 */1 * * * </p>
      	<br/>
      	<p>每天23:59执行一次 </p>
      	<p>59 23 * * *</p>
      	<br/>
      	<p>每天凌晨10分执行一次 </p>
      	<p>10 0 * * * </p>
      	<br/>
      	<p>每月最后一天的23:59执行 </p>
      	<p>29 23 L * * </p>
      	<br/>
      	<p>每天的9点到晚6点，每个小时执行一次 </p>
      	<p>1 9-18/1 * * * </p>
      	<br/>
      	<p>每天的早晨9点和晚上6点各执行一次 </p>
      	<p>1 9,18 * * * </p>
      	<br/>
      	<p>每天的早晨9点到晚上6点以后，每半个小时执行一次 </p>
      	<p>*/30 9-18 * * * </p>
      	<br/>
      	<p>每天的晚上10点到0点，每5分钟执行一次 </p>
      	<p>*/5 22-0 * * * </p>
      	<br/>
      	<p>每天的10点、12点、14点、16点、18点、20点、22点和0点各执行一次 </p>
      	<p>1 10-0/2 * * * </p>
      	<p>1 10,12,14,16,18,20,22,0 * * *</p>
      	
      </div> -->
      <div class="modal-footer">
        <button class="btn btn-default" data-dismiss="modal">取消</button>
        <button class="btn btn-primary" id="taskbtnSave">保存</button>
      </div>
    </div>
  </div>
</div>

<div th:replace="inc/footer"></div>
<script th:inline="javascript">
$(function(){
  var $modal = $('#taskSave');
  var $form = $('#taskformSave');
  $.ajax({
	  url:"/task/findAll",
	  success:function (result){
	    	console.log(result)
		  for(var i=0;i<result.length;i++){
			  var active = result[i].active;
			  var orderName = result[i].orderName;
		    	console.log(createtime)
			  var createtime = result[i].createTime;
		    	console.log(createtime)
		    	if(createtime){
		    		createtime = changeTime(createtime);
		    	}else{
		    		createtime = "";
		    	}
			  //var lastsynctime = result[i].lastsynctime?changeTime(result[i].lastsynctime):"";
			  if(orderName=="rollcall"){
				  orderName = "定时点名";
			  }else if(orderName == "shift"){
				  orderName = "定时值班";
			  }else{
				  orderName = "";
			  } 
			  if(active == true){
				  active = "正在运行";
			  }else{
				  active = "停止运行";
			  }       
			  //<td>'+lastsynctime+'</td>
			  $(".taskTable tbody").append('<tr><td>'+result[i].name+'</td><td>'+orderName+'</td><td>'+result[i].cronExpress+'</td><td>'+createtime+'</td><td>'+active+'</td><td><a class="btn btn-xs btn-danger btnDelete" myid="'+result[i].id+'">删除</a></td></td></tr>')
		  }
	  }
  });
  //显示添加模态框
  $("#taskAdd").click(function (){
	  $modal.modal('show');
  });
  //保存
  $("#taskbtnSave").on("click",function (){
    if (!$form.valid()) return;
    var data = $form.serializeDict();
    $.ajax({
      url: "/task/add",
      data: data,
      method: 'post',
      success: function (result) {
        alert('保存成功');
        window.location.reload();
      },
      error: function () {
        alert('保存失败');
      }
    })
  })
    // 删除

    $('.taskTable').on('click',".btnDelete", function () {
        var id = $(this).attr('myid');
        $.ajax({
          url: [[${#httpServletRequest.getContextPath()} +'/task/delete']],
          data: {
            id: id
          },
          method: 'post',
          success: function () {
            alert('删除成功');
            window.location.reload();
          },
          error: function () {
            alert('删除失败');
          }
        })

    })
  //更改时间格式
	function changeTime(time){
		var d = new Date(time);
		var mm = (d.getMonth() + 1)>9?(d.getMonth() + 1):"0"+(d.getMonth() + 1);
		var dd = (d.getDate())>9?(d.getDate()):"0"+(d.getDate());
		var hh = (d.getHours())>9?(d.getHours()):"0"+(d.getHours());
		var ms = (d.getMinutes())>9?(d.getMinutes()):"0"+(d.getMinutes());
		var ss = (d.getSeconds())>9?(d.getSeconds()):"0"+(d.getSeconds());
		var times=d.getFullYear() + '-' + mm + '-' + dd + ' ' + hh + ':' + ms + ':' + ss; 
		return times;
	}
});




</script>
</body>
</html>
